package com.example.lenovo.demo.presenter.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.lenovo.demo.DebarkActivity;
import com.example.lenovo.demo.R;
import com.example.lenovo.demo.UserActivity;
import com.example.lenovo.demo.model.databean.TravelsBean;
import com.example.lenovo.demo.view.customview.ExpandableTextView;

import org.xutils.image.ImageOptions;

import java.util.List;

/**
 * Created by Administrator on 2017/1/10.
 */

public class Travles_LvAdapter extends BaseAdapter implements View.OnClickListener {
    private List<TravelsBean.DataBean> data;
    private Context context;

    public Travles_LvAdapter(List<TravelsBean.DataBean> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            view = View.inflate(context, R.layout.travels_rv_item, null);
            vh = new ViewHolder();
            vh.iv_title = (ImageView) view.findViewById(R.id.rv_item_iv_title);
            vh.iv_btn = (CheckBox) view.findViewById(R.id.rv_item_iv_button);
            vh.iv_img = (ImageView) view.findViewById(R.id.rv_item_iv_bigimg);
            vh.btn_1 = (Button) view.findViewById(R.id.rv_item_btn_1);
            vh.btn_2 = (Button) view.findViewById(R.id.rv_item_btn_2);
            vh.btn_3 = (Button) view.findViewById(R.id.rv_item_btn_3);
            vh.tv_au = (TextView) view.findViewById(R.id.rv_item_tv_auchor);
            vh.tv_name = (TextView) view.findViewById(R.id.rv_item_tv_name);
            vh.tv_con = (ExpandableTextView) view.findViewById(R.id.expandable_text);
            vh.tv_num = (TextView) view.findViewById(R.id.rv_item_tv_number);
            vh.tv_sum = (TextView) view.findViewById(R.id.rv_item_tv_sum);
            vh.tv_star = (TextView) view.findViewById(R.id.rv_item_tv_starnum);
            vh.tv_title = (TextView) view.findViewById(R.id.rv_item_tv_title);
            vh.rv_img = (RecyclerView) view.findViewById(R.id.rv_item_rv_smallimg);
            vh.rv_btn = (RecyclerView) view.findViewById(R.id.rv_item_rv_button);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        if (data.size() > 0) {
            ImageOptions iob = new ImageOptions.Builder().setCircular(true).build();
            org.xutils.x.image().bind(vh.iv_title, data.get(i).getActivity().getUser().getPhoto_url(), iob);
            Glide.with(context).load(data.get(i).getActivity().getContents().get(0).getPhoto_url()).into(vh.iv_img);
            vh.tv_au.setText(data.get(i).getActivity().getUser().getName());
            vh.tv_name.setText(data.get(i).getUser().getName());
            vh.tv_con.setText(data.get(i).getActivity().getDescription());
            vh.tv_num.setText("旅行记");
            vh.tv_sum.setText(data.get(i).getActivity().getLikes_count() + "");
            vh.tv_star.setText(data.get(i).getActivity().getFavorites_count() + "");
            vh.tv_title.setText(data.get(i).getActivity().getTopic());
            Travles_ImgRvAdapter imgRvAdapter = new Travles_ImgRvAdapter(data.get(i).getActivity().getContents(), context);
            LinearLayoutManager llm = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            vh.rv_img.setLayoutManager(llm);
            vh.rv_img.setAdapter(imgRvAdapter);
            Travles_BtnRvAdapter btnRvAdapter = new Travles_BtnRvAdapter(data.get(i).getActivity().getDistricts(), context);
            LinearLayoutManager llm2 = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            vh.rv_btn.setLayoutManager(llm2);
            vh.rv_btn.setAdapter(btnRvAdapter);
        }
        vh.iv_img.setOnClickListener(this);
        vh.iv_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, UserActivity.class);
                intent.putExtra("id", data.get(i).getActivity().getUser().getId()+"");
                context.startActivity(intent);
            }
        });
        vh.iv_btn.setOnClickListener(this);
        vh.btn_1.setOnClickListener(this);
        vh.btn_2.setOnClickListener(this);
        vh.btn_3.setOnClickListener(this);
        vh.tv_au.setOnClickListener(this);
        vh.tv_name.setOnClickListener(this);
        vh.tv_star.setOnClickListener(this);
        vh.tv_num.setOnClickListener(this);
        vh.tv_sum.setOnClickListener(this);
        vh.tv_con.setOnClickListener(this);
        return view;
    }

    public void skip() {
        Intent intent = new Intent(context, DebarkActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rv_item_iv_title:

                break;
            case R.id.rv_item_iv_button:
                skip();
                break;
            case R.id.rv_item_iv_bigimg:
                Toast.makeText(context, "点击了bigimg", Toast.LENGTH_LONG).show();
                break;
            case R.id.rv_item_btn_1:
                skip();
                break;
            case R.id.rv_item_btn_2:
                skip();
                break;
            case R.id.rv_item_btn_3:
                skip();
                break;
            case R.id.rv_item_tv_auchor:
                Toast.makeText(context, "点击了auchor", Toast.LENGTH_LONG).show();
                break;
            case R.id.rv_item_tv_name:
                Toast.makeText(context, "点击了name", Toast.LENGTH_LONG).show();
                break;
            case R.id.expandable_text:
                Toast.makeText(context, "点击了content", Toast.LENGTH_LONG).show();
                break;
            case R.id.rv_item_tv_number:
                Toast.makeText(context, "点击了number", Toast.LENGTH_LONG).show();
                break;
            case R.id.rv_item_tv_sum:
                Toast.makeText(context, "点击了sum", Toast.LENGTH_LONG).show();
                break;
            case R.id.rv_item_tv_starnum:
                Toast.makeText(context, "点击了star", Toast.LENGTH_LONG).show();
                break;
        }

    }

    public class ViewHolder {
        ImageView iv_title, iv_img;
        Button btn_1, btn_2, btn_3;
        CheckBox iv_btn;
        TextView tv_au, tv_name, tv_num, tv_sum, tv_star, tv_title;
        ExpandableTextView tv_con;
        RecyclerView rv_img, rv_btn;
    }
}
